<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      html {
        height: 100%;
        display: flex;
        justify-content: center;
      }
      .preview {
        position: relative;
        width: 600px;
        height: 400px;
        overflow: hidden;
      }

      .img {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div class="preview">
      <img class="img" src="./test.png" alt="" />
    </div>
  </body>
  <script>
    const img = document.querySelector(".img");
    const preview = document.querySelector(".preview");
    const wrap = document.querySelector(".wrap");
    const imageParams = {
      naturalHeight: img.naturalHeight,
      naturalWidth: img.naturalWidth,
    };
    let factor = {
      translate: {
        x: 0,
        y: 0,
      },
      scale: 1,
      rotate: 0,
    };

    let spurs = [];

    preview.addEventListener("wheel", function (e) {
      console.log(
        "img",
        img.getBoundingClientRect().width,
        img.getBoundingClientRect().height
      );

      const x = e.clientX - preview.getBoundingClientRect().x;
      const y = e.clientY - preview.getBoundingClientRect().y;
      console.log(e.deltaY);
      const delta = -e.deltaY;
      let scaleVal = delta > 0 ? 0.2 : -0.2;

      if (factor.scale < 1) {
        factor = {
          translate: {
            x: 0,
            y: 0,
          },
          scale: 1,
          rotate: 0,
        };
      }
      factor.scale += scaleVal;

      console.log(e.clientX, e.clientY);

      console.log(x, y);

      //   console.log(scaleVal);
      const m = delta > 0 ? 0.1 : -0.1;

      const previewWidth = preview.getBoundingClientRect().width;
      const previewHeight = preview.getBoundingClientRect().height;

      //   console.log(previewWidth, previewHeight);

      factor.translate.x += -x * m * 2 + previewWidth * m;
      factor.translate.y += -y * m * 2 + previewHeight * m;

      console.log(factor);

      //   img.style.transform = `translateX(${factor.translate.x}px)`;
      //   img.style.transform = `translateY(${factor.translate.y}px)`;
      //   img.style.transform = `scale(${factor.scale})`;

      img.style.transform = `translate(${factor.translate.x}px, ${factor.translate.y}px) scale(${factor.scale})`;
      img.style.transformOrigin = "50% 50%";
    });

    preview.onclick = function (e) {
      console.log("img click", e.offsetX, e.offsetY, preview.clientWidth);

      const needWidth = e.offsetX / (preview.clientWidth / img.naturalWidth);
      const needHeight = e.offsetY / (preview.clientHeight / img.naturalHeight);

      console.log(needWidth, needHeight);

      const span = document.createElement("span");
      span.style.width = "10px";
      span.style.height = "10px";
      span.style.borderRadius = "5px";
      span.style.background = "red";
      span.style.position = "absolute";
      span.style.left = "10px";
      span.style.top = "10px";
      preview.appendChild(span);
    };

    const obj = new ResizeObserver(() => {
      console.log("resize");
    });
    obj.observe(img);
  </script>
</html>
